﻿using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.Ceki
{
    public class XPL
    {
        public int QtyPerBox = 8;
        public int QtyPerBoxMax = 9;
        public int QtyPerBigBox = 4;

        public int CustomerId, YuklemePartisiId, PLTemplateId;
        public string PlanlamaIds, Magaza, SayfaAdi;
        public bool BoxNrReset, BigBoxNrReset;
        public string SipKes;
        public DataSet dsData;
        public DataTable dtAdetler;
        public DataTable dtP = new DataTable();
        public DataTable dtPLines = new DataTable();
        public Dictionary<string, string> dx = new Dictionary<string, string>();

        public XPL()
        {
        }

        public XPL(int CustomerId, string PlanlamaIds, int YuklemePartisiId)
        {
            this.CustomerId = CustomerId;
            this.PlanlamaIds = PlanlamaIds;
            this.YuklemePartisiId = YuklemePartisiId;

            dtP.Columns.Add("idx", typeof(int));
            dtP.Columns.Add("YuklemePartisiId", typeof(int));
            dtP.Columns.Add("DagilimId", typeof(int));
            dtP.Columns.Add("Termin", typeof(DateTime));
            dtP.Columns.Add("Magaza", typeof(string));
            dtP.Columns.Add("SayfaAdi", typeof(string));
            dtP.Columns.Add("PLTemplateId", typeof(int));
            dtP.AddAutoIncrementalColumn("PackingListId");

            dtPLines.Columns.Add("idx", typeof(int));
            dtPLines.Columns.Add("PlanlamaId", typeof(int));
            dtPLines.Columns.Add("Brand", typeof(string));
            dtPLines.Columns.Add("Model", typeof(string));
            dtPLines.Columns.Add("DosyaNo", typeof(string));
            dtPLines.Columns.Add("BigBoxNr", typeof(int));
            dtPLines.Columns.Add("BoxNr", typeof(int));
            dtPLines.Columns.Add("Artikel", typeof(string));
            dtPLines.Columns.Add("Renk", typeof(string));
            dtPLines.Columns.Add("Magaza", typeof(string));
            dtPLines.Columns.Add("Ulke", typeof(string));
            dtPLines.Columns.Add("OrderNr", typeof(string));
            List<string> tpl = new List<string>();
            for (int i = 1; i < 29; i++)
            {
                string col = string.Format("A{0:00}", i);
                dtPLines.Columns.Add(col, typeof(int));
                if (i < 10)
                    dtPLines.Columns.Add("Ek" + i.ToStr(), typeof(string));

                tpl.Add("ISNULL(" + col + ",0)");
            }
            dtPLines.Columns.Add("Toplam", typeof(int), string.Join("+", tpl.ToArray()));

            dsData = Session.DataManager.ExecuteDataSet("GetYuklenecekAdetler", PlanlamaIds);

            EksikYapilandirma = dsData.Tables.Count < 4;

            if (dsData.Tables.Count > 2)
                foreach (DataRow r in dsData.Tables[2].Rows)
                    dx.Add(r["Caption"].ToStr(), r["COLUMN_NAME"].ToStr());

            if (dsData.Tables.Count > 3)
            {
                DataRow r = dsData.Tables[3].Rows[0];
                PLTemplateId = r["PLTemplateId"].ToInt();
                BoxNrReset = r["BoxNrReset"].ToStr() != string.Empty;
                BigBoxNrReset = r["BigBoxNrReset"].ToStr() != string.Empty;
                SipKes = r["SipKes"].ToStr();
                QtyPerBox = r["QtyPerBox"].ToInt();
                QtyPerBoxMax = r["QtyPerBoxMax"].ToInt();
                QtyPerBigBox = r["QtyPerBigBox"].ToInt();

                if (QtyPerBigBox == 0)
                    QtyPerBigBox = 1;
            }
        }

        public bool EksikYapilandirma = false;
        public void PLKaydet()
        {
            Session.DataManager.ExecuteNonQuery("InsPackingList", dtP.Serialize(), dtPLines.Serialize());
        }

    }
}


